package com.qms.repast.ui.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.qms.repast.R;
import com.qms.repast.commons.AppLocation;
import com.qms.repast.commons.Constants;
import com.qms.repast.entity.MessageEvent;
import com.qms.repast.entity.resbean.CityBean.BodyBean;
import com.qms.repast.entity.resbean.LocationBean;
import com.qms.repast.ui.adapter.CityListAdapter;
import com.qms.repast.ui.base.BaseActivity;
import com.qms.repast.ui.presenter.CityListPresenter;
import com.qms.repast.ui.view.ICityListView;
import com.qms.repast.utils.AppUtils;
import com.qms.repast.utils.SpUtils;
import com.qms.repast.weidgets.common.ClearEditText;
import com.qms.repast.weidgets.common.SideBar;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author 泅渡者
 * @date 2018/9/14 0014
 * @describe: 城市列表界面
 */
public class CityListActivity extends BaseActivity<CityListPresenter> implements ICityListView, SideBar.OnTouchingLetterChangedListener {

	@BindView(R.id.iv_back)
	ImageView ivBack;
	@BindView(R.id.tv_title)
	TextView tvTitle;
	@BindView(R.id.mClearEditText)
	ClearEditText mClearEditText;
	@BindView(R.id.mListView)
	ListView mListView;
	@BindView(R.id.mSideBar)
	SideBar mSideBar;
	TextView tvLocation;
	LinearLayout llLocation;

	/**
	 * 城市数据集合
	 */
	private List<BodyBean> findCityAllBeanList;
	/**
	 * 城市选择适配器
	 */
	private CityListAdapter adapter;

	@Override
	protected int initLayout () {
		return R.layout.activity_citylist;
	}

	@Override
	protected void initPresenter (Intent intent) {
		mPresenter = new CityListPresenter(this, this);
	}

	@Override
	protected void initView () {
		tvTitle.setText("城市选择");
		AppLocation.startLocation();
		View headView = View.inflate(this, R.layout.layout_citylist_head, null);
		tvLocation = headView.findViewById(R.id.tv_location);
		tvLocation.setText((String) SpUtils.getParam(AppUtils.getContext(), Constants.CITY, ""));
		llLocation = headView.findViewById(R.id.ll_location);
		llLocation.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick (View view) {
				EventBus.getDefault().post(new MessageEvent(1011, null));
				finish();
			}
		});
		mListView.addHeaderView(headView);
		findCityAllBeanList = new ArrayList<>();
		adapter = new CityListAdapter(AppUtils.getContext(), findCityAllBeanList);
		mListView.setAdapter(adapter);

		mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick (AdapterView<?> parent, View view, int position, long id) {
				BodyBean cityAllBean = adapter.getCurrentItem(position - 1);
				LocationBean locationBean = new LocationBean();
				locationBean.setCity(cityAllBean.getCity());
				locationBean.setCityId(cityAllBean.getCityId());
				//这里要和高德定位的数据做好合并处理，由于数据类型和后台数据类型不一致，此处只允许设置ID和NAME
				EventBus.getDefault().post(new MessageEvent(1011, locationBean));
				finish();
			}
		});
		mSideBar.setOnTouchingLetterChangedListener(this);
		mClearEditText.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged (CharSequence s, int start, int before, int count) {
				if (findCityAllBeanList != null && findCityAllBeanList.size() > 0) {
					mPresenter.filterDate(s.toString(), findCityAllBeanList);
				}
			}

			@Override
			public void beforeTextChanged (CharSequence s, int start, int count, int after) {

			}

			@Override
			public void afterTextChanged (Editable s) {
			}
		});

	}

	@Override
	protected void initData () {
		mPresenter.getCityList();
	}

	@OnClick(R.id.iv_back)
	public void onViewClicked () {
		finish();
	}

	@Override
	public void onTouchingLetterChanged (String s) {
		int position = adapter.getPositionForSection(s.charAt(0));
		if (position != -1) {
			mListView.setSelection(position);
		}
	}

	@Override
	protected void onDestroy () {
		super.onDestroy();
	}

	@Override
	public void updateData (List<BodyBean> filterDateList) {
		adapter.updateListView(filterDateList);
	}

	@Override
	public void cityListData (List<BodyBean> cityAllList) {
		findCityAllBeanList = cityAllList;
		llLocation.setVisibility(View.VISIBLE);
		adapter.updateListView(findCityAllBeanList);
	}
}
